<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentFieldConstructorOption.class.php, $Author:  $ */



/** User custom fields options list
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentFieldConstructorOption.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentFieldConstructorOption extends BaseComponent
{
    /** Processing method
     */
	function process()
	{
	    $oCustomFieldOptionItem = &$this->_oModule->getItem('CustomFieldOption');
	    /* @var $oCustomFieldOptionItem PhpFox_Mod_Account_ItemCustomFieldOption */
	    $oCustomFieldService = &$this->_oModule->getService('CustomField');
	    /* @var $oCustomFieldService PhpFox_Mod_Account_ServiceCustomField */
	    $oReq = &App::getRequest();
	    $oTpl = &$this->_oModule->getTemplate();

	    $iFieldId = $oReq->getInt('id');
	    if (!$iFieldId)
	    {
	        App::gotoUrl('admin.main.custom-fields');
	    }

	    if ( $iDelete = $oReq->getInt('idelete') )
	    {
	    	$oCustomFieldOptionItem->load($iDelete);
	    	$oCustomFieldOptionItem->delete($iDelete);
	    	Cache::subStrRemove('custom');
	    	App::gotoUrl('admin.main.custom-fields-option', array('id' => $iFieldId), App::format('item_deleted'), false);
	    }

	    if ($oReq->getAction() == 'save')
	    {
	        $aOptions = $oReq->getArray('option');
	        $aDelete = $oReq->getArray('delete');
	        $mDefault = $oReq->get('default');
	        foreach ($aDelete as $iOptionId)
	        {
	            $oCustomFieldOptionItem->load($iOptionId);
	            if (!$oCustomFieldOptionItem->aData['custom_option_is_default'])
	            {
	                $oCustomFieldOptionItem->delete($iOptionId);
	            }
	        }
	        foreach ($aOptions as $mOptionId => $sOption)
	        {
	            if (is_numeric($mOptionId))
	            {
	                $oCustomFieldOptionItem->load($mOptionId);
	                $oCustomFieldOptionItem->aData['custom_option_name'] = $sOption;
	                $oCustomFieldOptionItem->update(array('custom_option_name'));
	            }
	            elseif ($mOptionId != 'new_field')
	            {
	                $oCustomFieldOptionItem->aData['custom_option_id'] = '';
	                $oCustomFieldOptionItem->aData['custom_option_name'] = $sOption;
	                $oCustomFieldOptionItem->aData['custom_field_id'] = $iFieldId;
	                $iNewOptionId = $oCustomFieldOptionItem->insert();
	                if ($mDefault == $mOptionId)
    	            {
    	                $mDefault = $iNewOptionId;
    	            }
	            }
	        }
	        $oCustomFieldService->setDefault($iFieldId, $mDefault);
	        Cache::subStrRemove('custom');
	        App::gotoUrl('admin.main.custom-fields', array(), App::format('Mod_Account.field.updated'));
	    }

	    $aCond = array();
	    $aCond['custom_field_id'] = '= '.$iFieldId;
	    $sSort = 'custom_option_is_default DESC, custom_option_name';
	    list($aItems, $iCnt) = $oCustomFieldOptionItem->getList($aCond, 0, 0, $sSort);

	    $oTpl->assign(array(
	        'aItems'   => $aItems,
	        'iFieldId' => $iFieldId,
	    ));
	    return $oTpl->fetch('FieldOption.html');
	}
}
?>